﻿namespace Settings.NET.UnitTests.Helpers
{    
    using System.Collections.Generic;

    public class TestSettingsInMemory : ISettingsStorage
    {
        private readonly Dictionary<KeyScope, string> settings = new Dictionary<KeyScope, string>(new KeyScopeComparer());

        public ISetting<string> GetSetting(string key, string scope)
        {
            var keyScope = new KeyScope { Key = key, Scope = scope };
            var value = this.settings.ContainsKey(keyScope) ? this.settings[keyScope] : null;
            return new Setting<string>(key, value, value != null);
        }

        public void SetSetting(string key, string value, string scope)
        {
            var keyScope = new KeyScope { Key = key, Scope = scope };

            if (this.settings.ContainsKey(keyScope))
            {
                this.settings[keyScope] = value;
                return;
            }

            this.settings.Add(keyScope, value);
        }

        public void RemoveSetting(string key, string scope)
        {
            var keyScope = new KeyScope { Key = key, Scope = scope };

            if (this.settings.ContainsKey(keyScope))
            {
                this.settings.Remove(keyScope);
            }            
        }
    }
}